import axios from "axios";
import {
    createRouter,
    createWebHashHistory,
    createWebHistory,
} from "vue-router";
import Layout from "@/views/main.vue";
import { ElMessage } from "element-plus";
import { GetUserPermission } from "@/api/menu";
const routes = [
    {
        path: "",
        name: "",
        component: () => import("@/views/user/login.vue"),
    },
    {
        path: "/login",
        name: "login",
        component: () => import("@/views/user/login.vue"),
    },
    {
        path: "/pdf",
        name: "pdf",
        component: () => import("@/views/pdf/pdf.vue"),
    },
    {
        path: "/forgetPassword",
        name: "forgetPassword",
        component: () => import("@/views/user/forgetPassword.vue"),
    },
    {
        path: "/forgetUpdatePsssword",
        name: "forgetUpdatePassword",
        component: () => import("@/views/user/updatePassword_forget.vue"),
    },
    {
        path: "/user",
        component: Layout,
        children: [
            {
                path: "updatePassword",
                name: "updatePassword",
                component: () => import("@/views/user/updatePassword.vue"),
            },
            {
                path: "department",
                name: "department",
                component: () => import("@/views/department/index.vue"),
            },
            {
                path: "lesson",
                name: "lesson",
                component: () => import("@/views/lesson/index.vue"),
            },
            {
                path: "seal",
                name: "seal",
                component: () => import("@/views/seal/index.vue"),
            },
        ],
    },
    {
        path: "/template",
        component: Layout,
        children: [
            {
                path: "",
                name: "templateIndex",
                component: () => import("@/views/template/index.vue"),
            },
            {
                path: "category",
                name: "category",
                component: () => import("@/views/category/index.vue"),
            },
        ],
    },
    {
        path: "/flow",
        component: Layout,
        children: [
            {
                path: "",
                name: "flowIndex",
                component: () => import("@/views/flow/index.vue"),
            },
            {
                path: "node",
                name: "Flownode",
                component: () => import("@/views/Node/index.vue"),
            },
            {
                path: "setting",
                name: "flowsetting",
                component: () => import("@/views/flow/setting.vue"),
            },
            {
                path: "approver",
                name: "approver",
                component: () => import("@/views/flow/processApproval.vue"),
            },
            {
                path: "tranfer",
                name: "tranfer",
                component: () => import("@/views/tranfer/index.vue"),
            },
        ],
    },
    {
        path: "/permission",
        name: "permission",
        component: Layout,
        children: [
            {
                path: "",
                name: "permissionIndex",
                component: () => import("@/views/permission/Permission.vue"),
            },
            {
                path: "role",
                name: "rolePermission",
                component: () =>
                    import("@/views/permission/rolePermission.vue"),
            },
            {
                path: "user",
                name: "userPermission",
                component: () =>
                    import("@/views/permission/userPermission.vue"),
            },
        ],
    },
    {
        path: "/:carchAll(.*)",
        name: "404",
        component: () => import("@/views/error/404.vue"),
    },
];

// export async function fetchDynamicRoutes() {
//     axios.get("/api/menu/GetMenu").then((response) => {
//         if (response.data.code === 0) {
//             router.addRoute(response.data.data);
//         } else {
//             ElMessage({
//                 message: response.data.message,
//                 type: "error",
//             });
//         }
//     });
// }

export const GetMenu = () => {
    GetUserPermission()
        .then((response: any) => {
            console.log(response);
        })
        .catch(() => {});
};

const router = createRouter({
    // history: createMemoryHistory(), // Remove the empty string argument
    // history: createWebHashHistory(),
    history: createWebHistory(),
    routes,
});

/**
 * 检查Token是否过期
 * @returns Boolean
 */
const verifyToken = async () => {
    try {
        const response = await axios.post("/api/user/TokenValidator");
        return response.data.result;
    } catch (error) {
        // console.error(error);
        return false;
    }
};

router.beforeEach(async (to, _from, next) => {
    const nextRouteName = to.name;
    if (
        nextRouteName !== "login" &&
        nextRouteName !== "404" &&
        nextRouteName !== "forgetPassword" &&
        nextRouteName !== ""
    ) {
        const isLogin = await verifyToken();

        if (!isLogin) {
            localStorage.setItem(
                "nextRouter",
                nextRouteName ? nextRouteName.toString() : ""
            );
            ElMessage({
                message: "登录已过期，请重新登录",
                type: "error",
            });
            next({ name: "login" });
            return;
        }
        next();
    } else {
        next();
    }
});

export default router;
